package com.atguigu.mall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.mall.product.dto.AttrAttrgroupRelationDTO;
import com.atguigu.mall.product.entity.AttrGroupEntity;
import com.atguigu.mall.product.service.AttrGroupService;
import com.atguigu.mall.product.service.CategoryService;
import com.atguigu.mall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 * @author Mr·L
 * @email 907298149@qq.com
 * @date 2021-12-02 20:39:47
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(AttrGroupEntity attrGroup){
        PageUtils page = attrGroupService.queryPage(attrGroup);
        return R.ok().put("page", page);
    }

    /**
     * 查询分组的关联的属性 分页列表
     * @param params
     * @return
     */
    @RequestMapping("/list/relation/{attrGroupId}")
    public R attrGroupRelationAttrList(
            @PathVariable Long attrGroupId,
            @RequestParam Map<String,Object> params){
        PageUtils page = attrGroupService.attrGroupRelationAttrList(attrGroupId,params);
        return R.ok().put("page", page);
    }


    @RequestMapping("/list/notRelation/{attrGroupId}")
    public R attrGroupNotRelationAttrList(  @PathVariable Long attrGroupId,
                                            @RequestParam Map<String,Object> params){
        PageUtils page = attrGroupService.attrGroupNotRelationAttrList(attrGroupId,params);
        return R.ok().put("page", page);
    }


    @RequestMapping("/{catelogId}/withattr")
    public R withattr(  @PathVariable Long catelogId){
        List<AttrGroupWithAttrsVO> params = attrGroupService.withattr(catelogId);
        return R.ok().put("data", params);
    }


    /**
     * 添加 关联
     * @param attrAttrgroupRelationDTO
     * @return
     */
    @RequestMapping("/add/relation")
    public R relation(@RequestBody AttrAttrgroupRelationDTO attrAttrgroupRelationDTO ){
       attrGroupService.addRelation(attrAttrgroupRelationDTO);
        return R.ok();
    }


    @RequestMapping("/queryListByParams")
    public R queryListByParams(AttrGroupEntity attrGroup){
        List<AttrGroupEntity> list = attrGroupService.queryListByParams(attrGroup);
        return R.ok().put("data", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        List<Long> path = categoryService.queryPathListById(attrGroup.getCatelogId());
        attrGroup.setCategoryPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeAttrGroupByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

}
